package models.afd;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.GregorianCalendar;

public class AFDDao {
	
	private CabecalhoAFD cabecalhoAFD;
	private ArrayList<OperacaoAFD> operacaoAFD;
	private ArrayList<PontoAFD> pontoAFD;
		
	public AFDDao(String f) {
		cabecalhoAFD = new CabecalhoAFD();
		operacaoAFD = new ArrayList<OperacaoAFD>();
		pontoAFD = new ArrayList<PontoAFD>();
		File file = new File(f);
		try {
			FileInputStream ifs = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(ifs);
			BufferedReader br = new BufferedReader(isr);
			String line;
			while((line = br.readLine()) != null)
				ProcessarLinha(line);			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
	}

	private void ProcessarLinha(String line) {		
		switch(line.charAt(9)){
			case '1':{
				addCabecalho(line);
				break;
			}
			case '3':{
				addPonto(line);
				break;
			}
			case '5':{
				addOperacao(line);
				break;
			}
		}
	}

	private void addCabecalho(String line) {
		int d,M,y,h,m;
		cabecalhoAFD = new CabecalhoAFD();
		cabecalhoAFD.setTpCpfCnpj(Integer.parseInt(String.copyValueOf(line.toCharArray(),10,1)));
		cabecalhoAFD.setCpfCnpj(String.copyValueOf(line.toCharArray(),11,14));
		cabecalhoAFD.setNome(String.copyValueOf(line.toCharArray(),37,150));
		d = Integer.parseInt(String.copyValueOf(line.toCharArray(),204,2));
		M = Integer.parseInt(String.copyValueOf(line.toCharArray(),206,2))-1;
		y = Integer.parseInt(String.copyValueOf(line.toCharArray(),208,4))-1900;
		cabecalhoAFD.setRegIni(new GregorianCalendar(y,M,d).getTime());
		d = Integer.parseInt(String.copyValueOf(line.toCharArray(),212,2));
		M = Integer.parseInt(String.copyValueOf(line.toCharArray(),214,2))-1;
		y = Integer.parseInt(String.copyValueOf(line.toCharArray(),216,4))-1900;
		cabecalhoAFD.setRegFim(new GregorianCalendar(y,M,d).getTime());
		d = Integer.parseInt(String.copyValueOf(line.toCharArray(),220,2));
		M = Integer.parseInt(String.copyValueOf(line.toCharArray(),222,2))-1;
		y = Integer.parseInt(String.copyValueOf(line.toCharArray(),224,4))-1900;
		m = Integer.parseInt(String.copyValueOf(line.toCharArray(),228,2));
		h = Integer.parseInt(String.copyValueOf(line.toCharArray(),230,2));
		cabecalhoAFD.setDtGer(new GregorianCalendar(y,M,d,h,m).getTime());
	}

	private void addOperacao(String line) {
		int d,M,y,h,m;
		OperacaoAFD opeAFD = new OperacaoAFD();
		opeAFD.setNsr(Integer.parseInt(String.copyValueOf(line.toCharArray(),0,9)));
		d = Integer.parseInt(String.copyValueOf(line.toCharArray(),10,2))-1;
		M = Integer.parseInt(String.copyValueOf(line.toCharArray(),12,2))-1;
		y = Integer.parseInt(String.copyValueOf(line.toCharArray(),14,4));; 
		m = Integer.parseInt(String.copyValueOf(line.toCharArray(),18,2));
		h = Integer.parseInt(String.copyValueOf(line.toCharArray(),20,2));
		opeAFD.setDtInc(new GregorianCalendar(y,M,d,h,m).getTime());
		opeAFD.setTpOpe(line.charAt(22));
		opeAFD.setNrPis(String.copyValueOf(line.toCharArray(),23,12));
		opeAFD.setNome(String.copyValueOf(line.toCharArray(),35,52));
		operacaoAFD.add(opeAFD);
	}

	private void addPonto(String line) {
		int d,M,y,h,m;
		PontoAFD ptAFD = new PontoAFD();
		ptAFD.setNsr(Integer.parseInt(String.copyValueOf(line.toCharArray(),0,9)));
		d = Integer.parseInt(String.copyValueOf(line.toCharArray(),10,2))-1;
		M = Integer.parseInt(String.copyValueOf(line.toCharArray(),12,2))-1;
		y = Integer.parseInt(String.copyValueOf(line.toCharArray(),14,4));
		m = Integer.parseInt(String.copyValueOf(line.toCharArray(),18,2));
		h = Integer.parseInt(String.copyValueOf(line.toCharArray(),20,2));
		ptAFD.setDtPonto(new GregorianCalendar(y,M,d,h,m).getTime()); 
		ptAFD.setPis(String.copyValueOf(line.toCharArray(),22,12));
		pontoAFD.add(ptAFD);
	}

	public CabecalhoAFD getAfdCabecalho() {
		return cabecalhoAFD;
	}

	public OperacaoAFD getAfdInclusaos(int index) {
		return operacaoAFD.get(index);
	}
	
	public int getAfdIncSize(){
		return operacaoAFD.size();
	}

	public PontoAFD getAfdPontos(int index) {
		return pontoAFD.get(index);
	}

	public int getAfdPontoSize(){
		return pontoAFD.size();
	}

}
